package ru.ifmo.filemanager.ftpplugin;

import java.util.ArrayList;

import ru.ifmo.filemanager.filesystem.Type;
import ru.ifmo.filemanager.filesystem.interfaces.Directory;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryListReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.DirectoryLookupReceiver;
import ru.ifmo.filemanager.filesystem.interfaces.FileSystemObject;

public class FtpRootDirectory implements Directory {

	private final ArrayList<FtpTopDirectory> childrens = new ArrayList<FtpTopDirectory>();
	
	public void addConnection(FtpTopDirectory newDir)
	{
		childrens.add(newDir);
	}
	
	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Type type() {
		return Type.DIRECTORY;
	}
	

	@Override
	public String[] list() {
		String[] result = new String[childrens.size()];
		
		for(int i = 0; i < result.length; i++ )
		{
			result[i] = childrens.get(i).getServerName(); 
		}
		
		return result;
	}

	@Override
	public void listAsync(DirectoryListReceiver receiver) {
		receiver.directoryListSuccess(this, list());
	}

	@Override
	public FileSystemObject lookup(String name){
		
		for(FtpTopDirectory dir : childrens )
		{
			if( dir.getServerName().toLowerCase().equals( name.toLowerCase() ) )
				return dir; 
		}		
		
		return null;
	}

	@Override
	public void lookupAsync(String name, DirectoryLookupReceiver receiver) {
		receiver.directoryLookupSuccess(this, name, lookup(name));
	}

	@Override
	public String getName() {
		return "Ftp connections";
	}

}
